<?php
class NHSChoicesPluginContoller {
	public function NHSChoicesPluginContoller() {
		$this->db = new NHSChoicesDBContoller();
		$this->api = new NHSChoicesAPIContoller();
		
		add_shortcode('nhs-choices', array(&$this, 'handle_shortcode'));
		add_action('widgets_init', array(&$this, 'widgets'));
		add_action('init', array(&$this, 'init'));
		add_action('admin_menu', array(&$this, 'admin_menu'));
	}
	
	function no_api_key_warning() {
		?><div id="nhschoices-warning" class="updated fade">
			<p><strong><?php echo __('NHS Choices is almost ready.'); ?></strong>
			<?php echo sprintf(
				__('You must <a href="%1$s">enter your API key</a> for it to work.'),
				'options-general.php?page=nhs-choices-settings'
			) ?></p>
		</div><?php
	}
	
	function invalid_api_key_warning() {
		?><div id="nhschoices-warning" class="updated fade">
			<p><strong><?php echo __('NHS Choices has detected a problem.'); ?></strong> The API key being used appears to be invalid</p>
		</div><?php
	}
	
	function init() {
		if (!get_option('nhschoices_api_key')) {
			add_action('admin_notices', array(&$this, 'no_api_key_warning'));
		} else {
			$api_key = get_option('nhschoices_api_key');
			if(!get_option('nhschoices_username') || !get_option('nhschoices_password')) {
				if($this->api->obtain_credentials()) {
					add_action('admin_notices', array(&$this, 'invalid_api_key_warning'));
				}
			}
		}
	}
	
	function handle_shortcode($atts) {
		// TODO
	}
	
	function widgets() {
		register_widget('NHSChoicesWidgetController');
	}
	
	function admin_menu() {
		add_submenu_page(
			'options-general.php',
			'NHS Choices',
			'NHS Choices',
			'administrator',
			'nhs-choices-settings',
			array(&$this, 'settings')
		);
	}
	
	function settings() {
		require(
			dirname(__FILE__) . '/../views/settings.php'
		);
	}
} ?>